package com.example.rxjavademo.Net;

import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import com.example.rxjavademo.API.Api;
import com.example.rxjavademo.Bean.AllCity;
import com.example.rxjavademo.Bean.City;
import com.example.rxjavademo.MainActivity;
import com.example.rxjavademo.MyApplication;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author COCO
 * @date 2019/3/26
 */
public class RetrofitProxy {

    private static String baseUrl = "http://v.juhe.cn/weather/";

    private static Retrofit create() {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
        builder.readTimeout(10, TimeUnit.SECONDS);
        builder.connectTimeout(9, TimeUnit.SECONDS);

        return new Retrofit.Builder().baseUrl(baseUrl)
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    public static void sendRequest() {
        Retrofit retrofit = create();
        Api api = retrofit.create(Api.class);
        Observable<AllCity> observable = api.getAllCity("1");
        observable.subscribeOn(Schedulers.io())
                .flatMap(new Function<AllCity, ObservableSource<City>>() {
                    @Override
                    public ObservableSource<City> apply(AllCity city) throws Exception {
                        ArrayList<City> result = city.getResult();
                        return Observable.fromIterable(result);
                    }
                })
                .filter(new Predicate<City>() {
                    @Override
                    public boolean test(City city) throws Exception {
                        String id = city.getId();
                        if (Integer.parseInt(id) < 5) {
                            return true;
                        }
                        return false;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<City>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(City city) {
                        if (city == null) {
                            Toast.makeText(MyApplication.appContext, "data null", Toast.LENGTH_SHORT).show();
                        }else {
                            Log.i("HTTP", "data: " + city.toString());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("HTTP", "error: " + e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
